package msg

import (
	"cli/json"
	lua "github.com/yuin/gopher-lua"
)

type Message struct {
	Type  Type              `json:"type"`
	Code  int               `json:"code"`
	Func  string            `json:"func"`
	Info  string            `json:"info"`
	Param map[string]string `json:"param"`
}

func NewMessage(t Type, code int, f, info string, param map[string]string) *Message {
	return &Message{Type: t, Code: code, Func: f, Info: info, Param: param}
}

func (m *Message) String() string {
	s, _ := json.EncodeString(m)
	return s
}

func (m *Message) Param2String() string {
	s, _ := json.EncodeString(m.Param)
	return s
}

func (m *Message) Param2Table(l *lua.LState) lua.LValue {
	t := l.NewTable()
	for k, v := range m.Param {
		t.RawSetString(k, lua.LString(v))
	}
	return t
}
